import pymysql
import redis

redis_config = {'host': '192.168.1.136', 'port': 6379}
mysql_config = {'host': '192.168.1.136', 'port': 3306, 'passwd': 'Shai@123', 'user': 'root'}


# 返回redis对象
def get_redis_db(db=0):
    r = redis.StrictRedis(**redis_config, db=db)
    return r


# 设置redis值
def set_redis_value(key, value, db=0):
    r = get_redis_db(db)
    r.set(key, value)


# 执行数据库操作,需要在内网环境操作
def mysql(sql, db='db_ucenter'):
    db = pymysql.Connect(**mysql_config, db=db, charset='utf8')
    cursor = db.cursor()
    try:
        cursor.execute(sql)
        db.commit()
    except Exception as e:
        raise Exception(e)
    else:
        try:
            # data1=cursor.fetchone()#返回第一行
            data2 = cursor.fetchall()  # 返回所有结果
            return data2
        except Exception as e1:
            raise Exception(e1)


if __name__ == '__main__':
    r = get_redis_db(0)
    # # print(str(r.get('shai_main:verify_code:19920003264')).split('"')[1])
    # r.set('shai_main:verify_code:19920003265', r'"1212"')
    r = mysql(f'update user_info SET phone=NULL WHERE phone = "{16606007390}";')
    print(r)
    # print(len(r))
    # for i in r:
    #     print(i)
    # r.hset('shai_main:modify_info:60045582','intro','1')
